package com.qxzn.app.personalcenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.FaceIdListBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.personalcenter.adapter.FaceIDListAdapter;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 已上传的人脸列表
 */
public class FaceIDListActivity extends BaseActivity {

    @BindView(R2.id.lrv_face_id)
    LRecyclerView lrvFaceId;
    private LRecyclerViewAdapter recyclerViewAdapter;
    private FaceIDListAdapter adapter;
    private List<FaceIdListBean.DataBean> dataList = new ArrayList();
    private int page = 1;
    private int pageSize = 10;
    private Call<FaceIdListBean> call;

    @Override
    protected int initLayout() {
        return R.layout.activity_face_id;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("人脸识别");
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.mipmap.logo);
        iv_right.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        lrvFaceId.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new FaceIDListAdapter(this, dataList);
        recyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        lrvFaceId.setAdapter(recyclerViewAdapter);
        recyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivityForResult(new Intent(FaceIDListActivity.this, UploadFaceIDActivity.class).putExtra("data", dataList.get(position)), 1);
                AppUtil.showLog("点击位置：" + position);
            }
        });
        lrvFaceId.setLoadMoreEnabled(false);
        lrvFaceId.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppUtil.showLog("setOnRefreshListener");
                page = 1;
                getData();
            }
        });
        lrvFaceId.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                AppUtil.showLog("setOnLoadMoreListener");
                page++;
                getData();
            }
        });
        getData();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.iv_right) {
            startActivityForResult(new Intent(FaceIDListActivity.this, UploadFaceIDActivity.class), 1);
        }
    }

    private void getData() {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), "{\"device_id\":\"" + SharePreferenceUtil.getDeviceId(this) + "\",\"username\":\"" + SharePreferenceUtil.getUserName(this) + "\"}");
        call = HttpRequest.getService().faceIdList(body);
        call.enqueue(new Callback<FaceIdListBean>() {
            @Override
            public void onResponse(Call<FaceIdListBean> call, Response<FaceIdListBean> response) {
                lrvFaceId.refreshComplete(pageSize);
                FaceIdListBean keyBean = response.body();
                if (keyBean != null) {
                    if (keyBean.getCode() == 200) {
                        List<FaceIdListBean.DataBean> deviceList = keyBean.getData();
                        if (deviceList != null) {
                            if (page == 1) {
                                dataList.clear();
                            }
                            dataList.addAll(deviceList);
                            recyclerViewAdapter.notifyDataSetChanged();
                        }
                        return;
                    }
                }
                dataList.clear();
                recyclerViewAdapter.notifyDataSetChanged();
                if (keyBean != null && !TextUtils.isEmpty(keyBean.getMsg())) {
                    AppUtil.showToast(FaceIDListActivity.this, keyBean.getMsg());
                } else {
                    AppUtil.showToast(FaceIDListActivity.this, "网络错误，请稍后再试！");
                }
            }

            @Override
            public void onFailure(Call<FaceIdListBean> call, Throwable t) {
                AppUtil.showToast(FaceIDListActivity.this, "网络错误，请稍后再试！");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                getData();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (call != null) {
            call.cancel();
        }
    }
}
